import { Body, Controller, Delete, Get, Param, Post, Put, Inject, LoggerService, Logger, HttpException, HttpStatus, ForbiddenException, Query, UseFilters, UseGuards, Response, Request } from '@nestjs/common';
import { UserService } from './user.service';
import { ConfigService } from '@nestjs/config';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { getUserDto } from './dto/get-user.dto';
import { TypeormFilter } from 'src/filters/typeorm.filter';
import { AuthGuard } from '@nestjs/passport';
import { AdminGuard } from 'src/guards/admin/admin.guard';
import { Roles } from 'src/roles/roles.decorator';
import { RolesEnum } from 'src/roles/roles.enum';


@Controller('user')
@UseFilters(new TypeormFilter())
export class UserController {
  constructor(private readonly userService: UserService,
    private configService: ConfigService,
    @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: LoggerService,
  ) {}

  @Get()
  @Roles([RolesEnum.ADMIN])
  @UseGuards(AuthGuard('jwt'), AdminGuard)
  getUsers(@Query() query:getUserDto):any {
    Logger.log({ query: query })
    return this.userService.findAll(query)
  }

  @Get(":id")
  @UseGuards(AuthGuard('jwt'))
  getUser(@Param('id') id:string, @Request() request):any {
    console.log('request.user', request.user)
    return this.userService.findProfile(+id)
  }

  @Post()
  addUser(@Body() user: any):any {
    return this.userService.create(user)
  }

  @Put(':id')
  updateUser(@Param('id') id:string, @Body() user: getUserDto):any {
    return this.userService.update(+id, user)
  }

  @Delete(":id")
  deleteUser(@Param('id') id:string):any {
    return this.userService.remove(+id)
  }

  @Get("/logs/profile/:id")
  getLogsByUser(@Param('id') id:string):any {
    return this.userService.findLogs(+id)
  }

  @Get("/logs/group/:id")
  async getLogs(@Param('id') id:string):Promise<any> {
    const res = await this.userService.findLogsByGroup(+id)
    return res.map(o => ({
      result: o.result,
      count: o.count
    }))
  }
  
}
